package com.sh.service.impl;

import com.sh.dao.ParentDao;
import com.sh.dao.StudentDao;
import com.sh.service.ParentService;
import com.sh.service.RegisterService;
import com.sh.util.SmsUtil;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sh.dto.Response;
import sh.dto.ResultDto;
import sh.entity.Parent;
import sh.entity.Student;
import sh.exception.SystemException;


import javax.annotation.Resource;

@Service
@Transactional(rollbackFor = Throwable.class)
public class ResgisterServiceImpl implements RegisterService {
    @Resource
    private SmsUtil smsUtil;
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private StudentDao studentDao;
    @Resource
    private ParentDao parentDao;
    @Autowired
    private ParentService parentService;
    @Override
    /**
     * 发送短信方法
     */
    public ResultDto sendMessage(Parent parent) {
       // System.out.println(phone);
        String phone=parent.getParentPhone();
        if (phone==null||phone=="") {
            return Response.error(1001,"电话号码不能为空");
        }
        if (!phone.matches("^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$")) {
            return Response.error(1002,"电话号码格式不正确");
        }

        String code = smsUtil.getCode();
        Boolean aBoolean = smsUtil.sendMessage(phone, code);
        if (!aBoolean) {
            return Response.error(1003,"发送验证码错误,请再次注册");
        }
        redisTemplate.opsForValue().set(phone,phone+"-"+code);

        return Response.success(200,code);
    }

    /**
     * 注册方法
     * @param parent
     * @return
     */
    @Override
    public ResultDto regist(Parent parent) {
        String str1 = (String) redisTemplate.opsForValue().get(parent.getParentPhone());
        String phone=str1.substring(0,str1.indexOf("-"));
        String code= str1.substring(str1.indexOf("-"));
        if (parent.getParentPhone()!=phone||parent.getVertifyCode()!=code) {
            return new ResultDto(1004,"手机号或者验证码错误");
        }

        Parent dbParent = parentDao.findByParentPhone(parent.getParentPhone());
        if (dbParent!=null) {
            return new ResultDto(1006,"已有家长用这个手机号注册");
        }
        Student dbStudent = studentDao.findByStudentCode(parent.getStudentCode());
        if (dbStudent==null) {
            return Response.error(1005,"没有这个学号的学生");
        }

        Parent daoByParentName = parentDao.findByParentName(parent.getParentName());
        if (daoByParentName!=null) {
            return Response.error(1005,"该用户名已经存在");
        }

        Parent insertParent = parentService.insert(parent);
        if (parent==null) {
            throw  new SystemException(1007,"注册失败，请重新注册");
            //return Response.error(1007,"注册失败，请重新注册");
        }
        int relation=1;
        if (parent.getParentSex()=="m") {
            relation=0;
        }
        int  addParentAndStudentR= parentDao.addParentAndStudentR(insertParent.getParentId(), dbStudent.getStudentId(), relation);
        if (addParentAndStudentR==0) {
            throw  new SystemException(1008,"注册失败，请重新注册");
        }
        return Response.success(200,"注册成功");
    }


}
